package com.kye.express.business.invoice_online.detail;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding2.view.RxView;
import com.kye.base.DebounceObservableTransformer;
import com.kye.express.R;
import com.kye.express.databinding.ItemInvoiceDetailBinding;
import com.kye.model.bean.invoice_online.GetInvoiceDetailResultBean;

import io.reactivex.functions.Consumer;

class InvoiceDetailtemViewBinder extends me.drakeet.multitype.ItemViewBinder<GetInvoiceDetailResultBean.SourceCodeDataBean, InvoiceDetailtemViewBinder.ViewHolder> {

    private Context mContext;
    private InvoiceDetailListener mListener = null;

    public InvoiceDetailtemViewBinder(Context context, InvoiceDetailListener listener) {
        mContext = context;
        mListener = listener;
    }

    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(@NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View view = inflater.inflate(R.layout.item_invoice_detail, parent, false);
        return new ViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull GetInvoiceDetailResultBean.SourceCodeDataBean item) {
        holder.bindData(item);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private GetInvoiceDetailResultBean.SourceCodeDataBean data;
        ItemInvoiceDetailBinding dataBinding;

        public ViewHolder(View itemView) {
            super(itemView);
            dataBinding = DataBindingUtil.bind(itemView);
        }


        public void bindData(GetInvoiceDetailResultBean.SourceCodeDataBean item) {
            data = item;
            dataBinding.setData(data);
            dataBinding.tvPrice.setText("￥"+data.getInvoicedAmount());
            RxView.clicks(dataBinding.ivDownload)
                    .compose(new DebounceObservableTransformer<>())
                    .subscribe(new Consumer<Object>() {

                        @Override
                        public void accept(Object o) throws Exception {
                            if (mListener != null) {
                                mListener.onOpenInvoice(item);
                            }
                        }
                    });
        }
    }
}
